package com.hn.zhijian.log;

import com.hn.zhijian.base.log.annotation.Log;
import com.hn.zhijian.base.log.aop.LogPoint;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * 日志切面处理类
 *
 * @author carl chen
 * @create 2019-10-22
 */
@Aspect
@Slf4j
@Data
@Component
public class LogAspect {
    /**
     * 日志切入点
     */
    @Autowired
    private LogPoint logPoint;

    /**
     * 保存系统操作日志
     *
     * @return 方法执行结果
     * @throws Throwable 调用出错
     */
    @Pointcut("@annotation(com.hn.zhijian.base.log.annotation.Log)")
    public void save() throws Throwable {
        /**
         * 解析Log注解
         */
//        String methodName = joinPoint.getSignature().getName();
//        Method method = currentMethod(joinPoint, methodName);
//        Log log = method.getAnnotation(Log.class);
//
//        /**
//         * 日志入库
//         */
//        if (log != null) {
//            logPoint.save(joinPoint, methodName, log.module(), log.description());
//        }

        System.out.println("插入之后");
        /**
         * 方法执行
         */
//        return joinPoint.proceed();
    }




    @Around(value = "save()")
    public void save(ProceedingJoinPoint joinPoint) throws Throwable {
        /**
         * 解析Log注解
         */
        String methodName = joinPoint.getSignature().getName();
        Method method = currentMethod(joinPoint, methodName);
        Log log = method.getAnnotation(Log.class);
        System.out.println(log.moduleKey()+"-------------------------------------------------------------------");

        /**
         * 日志入库
         */
//        if (log != null) {
//            logPoint.save(joinPoint, methodName, log.module(), log.description());
//        }

        /**
         * 方法执行
         */
    }


//    @Around("save()")
    public void after(JoinPoint joinPoint){
        System.out.println("方法之后执行...after.");
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        log.info("<=====================================================");
        log.info("请求来源： =》" + request.getRemoteAddr());
        log.info("请求URL：" + request.getRequestURL().toString());
        log.info("请求方式：" + request.getMethod());
        log.info("响应方法：" + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        log.info("请求参数：" + Arrays.toString(joinPoint.getArgs()));
        log.info("------------------------------------------------------");
        System.out.println(Arrays.toString(joinPoint.getArgs()));
    }

    /**
     * 获取当前执行的方法
     *
     * @param joinPoint  连接点
     * @param methodName 方法名称
     * @return 方法
     */
    private Method currentMethod(ProceedingJoinPoint joinPoint, String methodName) {
        /**
         * 获取目标类的所有方法，找到当前要执行的方法
         */
        Method[] methods = joinPoint.getTarget().getClass().getMethods();
        Method resultMethod = null;
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                resultMethod = method;
                break;
            }
        }
        return resultMethod;
    }
}
